/*
* Copyright 2010 ANDAGO INGENIERIA S.L.
*
* Licensed under the EUPL, Version 1.1 only (the "Licence");
* You may not use this work except in compliance with the
* Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in
* writing, software distributed under the Licence is
* distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the Licence for the specific language governing
* permissions and limitations under the Licence.
*/

package com.andago.alohaui;


import java.util.List;

import com.andago.utils.NomenclatureIEEE;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class MeasuresListActivity extends Activity {
	
	
	private String selection;
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		
		super.onCreate(savedInstanceState);
		
		 final String[] MEASURES = new String[] {
					this.getString(R.string.temperature), 
					this.getString(R.string.weight), 
					this.getString(R.string.oxigen_saturation), 
					this.getString(R.string.heart_rythm),
					this.getString(R.string.glucose)
					//this.getString(R.string.blood_preassure)
			    };
		 
		 Bundle extras = getIntent().getExtras();
	     if (getIntent().getExtras()!=null){
	    	   selection = extras.getString(getString(R.string.selection_bundle));
	    	   
	     }
	       
		setContentView(R.layout.main_aloha);
		
		ListView lv = (ListView) findViewById(R.id.list_measures);
		lv.setAdapter(new ArrayAdapter<String>(this, R.layout.fila, MEASURES));
		 
	    lv.setTextFilterEnabled(true);
	   	
	    lv.setSelection(0);
	      
	    lv.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView<?> parent, View view,
	            int position, long id) {
	        	
	        		if(startingService()){
	        			Intent intent = new Intent(MeasuresListActivity.this,DevicesActivity.class);
	        			intent.putExtra(getString(R.string.measurement_bundle), NomenclatureIEEE.getAttributeCode(MEASURES[position]));
	        			intent.putExtra(getString(R.string.selection_bundle), selection);
	        			startActivityForResult(intent,0);
	        		}
	        	
	        }
	      });
    
		
	}
    
     
	 private boolean startingService (){
		 
		  boolean connected = false;
		 
	    	// Make sure the service is started.  It will continue running
	        // until someone calls stopService().
	        // We use an action code here, instead of explictly supplying
	        // the component name, so that other packages can replace
	        // the service.
		 
		 
		// checking whether the activity is already started
	        ActivityManager a = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
	        List<RunningServiceInfo> servicesInfo= a.getRunningServices(15);
	        
	        
	        for (RunningServiceInfo ele : servicesInfo) {
	        	
	        	System.err.println("Process : " + ele.process);
	        	
		        if (ele.process.equals("es.libresoft.openhealth.android:remote")) {
		        	
		        	System.err.println("Ha entrado Procesos : " + ele.process);		        	
		        	if(!ele.started){
		        		System.err.println("service not started!!");
	        	    	Intent intentDroid = new Intent(getString(R.string.droidEvent));
	        	    		
	        	    	if (startService(intentDroid)==null){
	        	    		Toast toast = Toast.makeText(this, R.string.not_found_service, Toast.LENGTH_SHORT);
	        	    		toast.show();
	        	    		
	        	    	}else{
	        	    		connected=true;
	        	    		System.err.println("service started!!");
	        	    	}
	        	    }else{
	        	    	connected=true;	        	    	        	    	
	        	    	System.err.println("service already started!!");
	        	    	
	        	    }
	        	    	
		        }
	         } 	

	        if (!connected){
	        	System.err.println("service not started!!");
    	    	Intent intentDroid = new Intent(getString(R.string.droidEvent));
    	    	if (startService(intentDroid)==null){
    	    		Toast toast = Toast.makeText(this, R.string.not_found_service, Toast.LENGTH_SHORT);
    	    		toast.show();
    	    		
    	    	}else
    	    	{
    	    		connected=true;
    	    		System.err.println("service started!!");
    	    	}	
	         }
	        
	        return connected;
	        	
	 }
	 
	

	 @Override
		public void onStop(){
			super.onStop();
			this.finish();
		}
		@Override
		public void onDestroy(){
			super.onDestroy();
		}	 
}

